<template>
  <div class="department-container">
    <div class="department-search">
      <el-form :inline="true" :model="searchform" ref="searchform" class="demo-form-inline">
        <el-form-item label="部门查询" prop="key">
          <el-input v-model="searchform.key" placeholder="部门查询" size="mini"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="list">查询</el-button>
          <el-button type="primary" size="mini" @click="resetForm('searchform')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="department-body">
      <el-button type="primary" size="mini" style="margin-bottom: 20px;" @click="addDept">添加部门</el-button>
      <el-table
        :data="department"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column prop="dname" label="部门名称" width="180"></el-table-column>
        <el-table-column prop="status" label="状态" width="180">
          <template v-slot="{row}">
            <el-switch
              v-model="row.status"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="descript" label="备注"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 新增 / 编辑 -->
    <add-and-update
      ref="addAndUpdate"
      v-model="dialogVisible"
      :departments="this.department"
      @refresh="refresh"
    ></add-and-update>
  </div>
</template>

<script>
import { list, del } from "@/api/department";
import addAndUpdate from "./add-and-update/index.vue";
export default {
  name: "department",
  components: {
    addAndUpdate
  },
  data() {
    return {
      searchform: {
        key: ""
      },
      department: [
        // {
        //   id: 1,
        //   dPId: 0,
        //   status: 0,
        //   dName: "王小虎",
        //   desc: "上海市普陀区金沙江路 1518 弄",
        //   createTime: "2016-05-02"
        // }
      ],
      dialogVisible: false
    };
  },
  created() {
    this.list();
  },
  methods: {
    async list() {
      const { data } = await list(this.searchform);
      this.department = data;
    },
    addDept() {
      this.dialogVisible = true;
    },
    handleEdit(index, row) {
      this.$refs["addAndUpdate"].init(row);
      this.$nextTick(() => {
        this.dialogVisible = true;
      });
    },
    handleDelete(index, row) {
      if (row.children.length > 0) {
        this.$message({
          type: "warning",
          message: "部门下面存在子级部门，禁止删除"
        });
      } else {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(async () => {
            await del(row.id);
            this.list();
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      }
    },
    resetForm(formname) {
      this.$refs[formname].resetFields();
    },
    refresh() {
      this.dialogVisible = false;
      this.list();
    }
  }
};
</script>

<style lang="scss" scoped>
.department {
  &-container {
    margin: 30px;
  }

  &-search {
    height: 70px;
    padding: 10px 10px;
    border-radius: 5px;
    background-color: #fff;
  }

  &-body {
    border-radius: 5px;
    background-color: #fff;
    margin: 20px 0px;
    padding: 20px 10px;
  }
}
</style>